package com.sywb.chuangyebao.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.BusinessBean;
import com.sywb.chuangyebao.bean.ConsultBean;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.widget.RichTextView;

import org.bining.footstone.adapter.BaseRecyclerMultiItemAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.log.Logger;
import org.bining.footstone.view.BaseFragment;

/**
 * Created by pxb on 2018/6/14.
 *加盟咨询
 */
public class ConsultationFragment extends BaseStatisticsFragment{
    private BusinessBean businessBean;
    RecyclerView commonRecycler;
    private QuestionAdapter questionAdapter = null;
    /**
     * Fragment 实例
     */
    public static ConsultationFragment newInstance(Object... pramars) {
        ConsultationFragment fragment = new ConsultationFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
        if (bundle==null){
          bundle = getArguments();
        }
        businessBean =(BusinessBean) bundle.getSerializable("p0");
        commonRecycler = (RecyclerView) getView(R.id.common_recycler);
        questionAdapter = new QuestionAdapter(mActivity);
        commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
        commonRecycler.setAdapter(questionAdapter);
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        getData();
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putSerializable("p0",businessBean);
        super.onSaveInstanceState(outState);
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.layout_business;
    }

    private void getData(){
        for (ConsultBean countBean:businessBean.getConsult()) {
            countBean.getContent().setItemType();
            countBean.setItemType(countBean.getContent().getItemType());
        }
        questionAdapter.setDatas(businessBean.getConsult());
    }

    //加盟咨询
    class QuestionAdapter extends BaseRecyclerMultiItemAdapter<ConsultBean> {
        public QuestionAdapter(Context context) {
            super(context, null);
            addItemType(Constants.QANDA_TEXT, R.layout.item_user_dynamic_text);
            addItemType(Constants.QANDA_PIC_ONE, R.layout.item_user_dynamic_pic_one);
            addItemType(Constants.QANDA_PIC_THREE, R.layout.item_user_dynamic_pic_three);
            addItemType(Constants.QANDA_MEDIA, R.layout.item_user_dynamic_media);
            addItemType(Constants.USER_LIST, R.layout.layout_tutor_recommend);
        }
        @Override
        protected int getDefItemViewType(int position) {
            if (position >= getDataCount()) {
                return 0x00000333;
            }
            return super.getDefItemViewType(position);
        }
        @Override
        protected void setMultiData(final ViewHolderHelper viewHolderHelper, final int i, final ConsultBean model) {
            Log.e("ad",model.getContent().getItemType()+"");
            if (model.getContent().getItemType() != Constants.USER_LIST) {
                //包括头部和富文本
                //setHeadPart(viewHolderHelper, i, model);
//                    setBottomPart(viewHolderHelper, model);
                viewHolderHelper.setItemChildClickListener(R.id.iv_face);
                viewHolderHelper.setItemChildClickListener(R.id.iv_user_type);
                viewHolderHelper.setItemChildClickListener(R.id.tv_collection_name);
                viewHolderHelper.setItemChildClickListener(R.id.tv_collection_time);
                viewHolderHelper.setItemChildClickListener(R.id.tv_bottom_right);
                viewHolderHelper.setItemChildClickListener(R.id.richTextView);

                //设置富文本
                RichTextView richTextView = viewHolderHelper.getView(R.id.richTextView);

                switch (model.getContent().getItemType()) {
                    case Constants.QANDA_PIC_ONE:
                        viewHolderHelper.setItemChildClickListener(R.id.iv_img);
                        //设置图片（一张）
                        ImageView imageView = viewHolderHelper.getView(R.id.iv_img);
                        GlideUtils.getNormalIamgeToUrl(mActivity,(String)model.getContent().getPic().get(0), imageView);

                        richTextView.setDeviationDP(R.dimen.dp_172);
                        break;
                    case Constants.QANDA_PIC_THREE:
                        viewHolderHelper.setItemChildClickListener(R.id.iv_img0);
                        viewHolderHelper.setItemChildClickListener(R.id.iv_img1);
                        viewHolderHelper.setItemChildClickListener(R.id.iv_img2);
                        if (model.getContent().getPic() != null && model.getContent().getPic().size() > 0) {
                            ImageView imageView0 = viewHolderHelper.getView(R.id.iv_img0);
                            GlideUtils.getNormalIamgeToUrl(mActivity,(String)model.getContent().getPic().get(0), imageView0);
                            ImageView imageView1 = viewHolderHelper.getView(R.id.iv_img1);
                            if (model.getContent().getPic().size() > 1) {
                                imageView1.setVisibility(View.VISIBLE);
                                GlideUtils.getNormalIamgeToUrl(mActivity,(String) model.getContent().getPic().get(1), imageView1);
                                ImageView imageView2 = viewHolderHelper.getView(R.id.iv_img2);
                                if (model.getContent().getPic().size() > 2) {
                                    imageView2.setVisibility(View.VISIBLE);
                                    GlideUtils.getNormalIamgeToUrl(mActivity,(String) model.getContent().getPic().get(2), imageView2);
                                } else {
                                    imageView2.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                imageView1.setVisibility(View.INVISIBLE);
                            }
                        }
                        break;
                    case Constants.QANDA_MEDIA:
                        //Utils.setQuestionAudioItemView(viewHolderHelper, mActivity, model, playingAudioUrl, mp3Player, isPlaying);
                        break;
                }


                richTextView.setRichText(model.getContent().getTitle(), null);
                richTextView.setExpandView(true);
                richTextView.setOnLinkClickListener(new RichTextView.OnLinkClickListener() {
                    @Override
                    public void onAtClick(RichTextView richTextView, Object userId) {
//                            mView.advance(UserIndexActivity.class, userId);
                        Utils.goUserIndex(mActivity, (Integer) userId);
                    }

                    @Override
                    public void onTopicClick(RichTextView richTextView, Object topicId) {
                        //若当前已为话题聚合页，则不跳转
                    }

                    @Override
                    public void onEmojiClick(RichTextView richTextView, Object emoji) {

                    }

                    @Override
                    public void onUrlClick(RichTextView richTextView, Object url) {

                    }

                    @Override
                    public void onAllClick(RichTextView richTextView) {

                    }
                });

            }
        }
    }
}
